package com.wmyg.antifake.annotation;

import com.google.zxing.BarcodeFormat;
import com.wmyg.antifake.bean.Address;
import com.wmyg.antifake.bean.IpQrDetails;
import com.wmyg.antifake.bean.QueryInfo;
import com.wmyg.antifake.dao.IpQrDetailsMapper;
import com.wmyg.antifake.utils.GeoLiteUtil;
import com.wmyg.antifake.utils.GetLocationBaiduMap;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

@Slf4j
@Aspect
@Component
public class IPCheckSen {

    @Autowired
    IpQrDetailsMapper ipQrDetailsMapper;
    @Value("${max.meter}")
    private Integer meter;
    private static Map resultMap ;

    @Pointcut("@annotation(com.wmyg.antifake.annotation.IPCheck)")
    public void iPCheckParam() {

    }

    @Around("iPCheckParam()")
    public Object executeParam(ProceedingJoinPoint pjp) throws Throwable {
        Object[] obj = pjp.getArgs();
        for(int i = 0 ; i<obj.length;i++){
            System.out.println("接收参数=================================="+obj[i]);
        }
        HttpServletRequest request = (HttpServletRequest)obj[6];
        String addIP = request.getHeader("client-ip");
        if(obj[4].equals(BarcodeFormat.QR_CODE.name())){
            if(0.0 != (float)obj[3])
                //TODO 用户开启GPS定位功能 根据经纬度获取具体地址信息
                resultMap = GetLocationBaiduMap.getLocationByBaiduMap(obj[2].toString(),obj[3].toString());
            else
                //TODO 用户未开启GPS定位功能 根据IP获取具体地址信息
                resultMap = GeoLiteUtil.getGeoLite(addIP);
                //TODO 判断最后一次相同请求位置信息差与时间差是否大于1000m/s
        }
        ipQrDetailsMapper.insertSelective(setIpQrDetails(resultMap,addIP,obj[0].toString(),obj[4].toString()));
        return pjp.proceed();
    }

    /**
     *
     * 异常通知：目标方法抛出异常时执行的代码
     * 可以访问到异常对象
     * @param jp
     * @param ex
     */
    public void doAfterThrowing(JoinPoint jp, Exception ex){
        System.out.println("===========执行异常通知============"+ex);
    }


    private IpQrDetails setIpQrDetails(Map  resultMap , String addIp , String qrCode, String qrType){
        IpQrDetails qrDetails = new IpQrDetails();
        qrDetails.setCountry(resultMap.get("country").toString());
        qrDetails.setProvince(resultMap.get("province").toString());
        qrDetails.setCity(resultMap.get("city").toString());
        qrDetails.setCounty(resultMap.get("county").toString());
        qrDetails.setDetailsAddress(resultMap.get("details_address").toString());
        qrDetails.setLongitude(resultMap.get("longitude").toString());
        qrDetails.setLatitude(resultMap.get("latitude").toString());
        qrDetails.setReqType(StringUtils.isEmpty(addIp)?1:2);
        qrDetails.setReqTime(new Date());
        qrDetails.setIpAddress(addIp);
        qrDetails.setQrCode(qrCode);
        qrDetails.setQrType(qrType);
        return qrDetails;
    }
}
